﻿using Architecture.CrossCutting.Common;
using Architecture.CrossCutting.Web.WCF;
using Architecture.Domain.TestModel.Samples;
using Architecture.Services.ServicesInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.ServiceClient.Console
{
   public class WcfClient
   {
        public Task<List<Organisation>> GetOrganisations()
        {

            //BasicHttpBinding binding = new BasicHttpBinding("simpleClient");


            //            ChannelFactory<IOrganisationService> factory = new ChannelFactory<IOrganisationService>(binding);

            ChannelFactory<IOrganisationService> factory = new ChannelFactory<IOrganisationService>("IOrganisationServiceClient");

            //var messageInspector = CustomContext.Current.DependencyResolver.GetService<ClientMessageInspector>();
            //factory.Endpoint.EndpointBehaviors.Add(messageInspector);

            //var extension = new PipelineExtension();

            //foreach (var ope in factory.Endpoint.Contract.Operations)
            //{
            //    //                ope.OperationBehaviors.Add(CustomContext.Current.DependencyResolver.GetService <ParameterInspector>());
            //    var paramInspector = CustomContext.Current.DependencyResolver.GetService<ParameterInspector>();
            //    paramInspector.Extension = extension;
            //    ope.OperationBehaviors.Add(paramInspector);
            //}

            //EndpointAddress address = new EndpointAddress("http://localhost/WcfServices/OrganisationService.svc");
            //IOrganisationService svc = factory.CreateChannel(address);
            IOrganisationService svc = factory.CreateChannel();
            ((IClientChannel)svc).Closed += (s, e) =>
            {
                factory.Close();
            };


            //((IClientChannel)svc).Extensions.Add(new PipelineExtension());
            //((IClientChannel)svc).Extensions.Add(extension);

            Task<List<Organisation>> task = new Task<List<Organisation>>(() =>
            {
                //System.Console.WriteLine("Task has been started on ThreadId {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
                return svc.GetOrganisations().ToList();
            });

            task.Start();
            return task;
        }

    }
}
